import { lk_buyer,lk_common,lk_expert,lk_seller,lk_manager,lk_consumer } from './myConfig.js';
function request(method, url, data, header) {
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				//  uni.hideLoading() 
				if (res && res.code.code === 200) {
					try {
						const _data_ = JSON.parse(res.data || '{}');
						resolve(_data_);
					} catch (error) {
						resolve(res.data);
					}
				} else {
					uni.showToast({
						title: res.message || '服务器响应超时,请稍后再试！',
						icon: 'none'
					});
					reject(res.message);
				}
			},
			fail: function(res) {
				console.log(res, 9999)
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res.data);
			}
		});
	})
}

function requesthttpLogin(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requesthttp(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function(res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestlist(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestlistX(method, url, data, header).then(function(data) {
				resolve(data);
			}).catch(function(data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestlistX(method, url, data, header).then(function(data) {
						resolve(data);
					}).catch(function(data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestlistX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}
//单点登录
function requestRight(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken'),
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url: url,
			method,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function(res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}


export const lzyApis = {
	/**
	 * @param { }
	 * @description 获取当前登录用户课程
	 */
	getUserCourseAsync: (data) => requesthttp('POST', `${lk_buyer}/buyer/user/userCourse/myList`, data, {
		'content-type': 'application/json'
	}),
	/**
	 * @param { }
	 * @description 获取当前挂号单的下载附件
	 */
	prescriptionDetailsAsync: (data) => requesthttp('GET', `${lk_buyer}/buyer/user/userRegister/prescriptionDetails/${data.id}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 下载附件
	 */
	userDownloadAsync:(url)=>requesthttp('POST', `${lk_common}/common/common/download`, url, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 取消基地预约
	 */
	cancelOrderBookingAsync:(data)=>requesthttp('POST', `${lk_buyer}/buyer/user/userTrainingBase/cancelOrderBooking`, data, {
		'content-type': 'application/json'
	}),
	/**
	 * @param { }
	 * @description 删除基地预约
	 */
	delOrderBookingAsync:(data)=>requesthttp('DELETE', `${lk_buyer}/buyer/user/userTrainingBase/${data.id}`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 取消专家关注
	 */
	delIdolAsync:(data)=>requesthttp('DELETE', `${lk_buyer}/buyer/passport/member/user/idol`, data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 根据id查患者信息
	 */
	findRelationUserByIdAsync:(data)=>requesthttp('POST', `${lk_buyer}/buyer/passport/member/user/findRelationUserById/${data.id}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 修改密码
	 */
	modifyPassAsync:(data)=>requesthttp('PUT', `${lk_buyer}/buyer/passport/member/modifyPass`,data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 请求验证码
	 */
	smsAsync:(data)=>requesthttp('GET', `${lk_common}/common/common/sms/${data.verificationEnums}/${data.mobile}`,data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 验证用户手机号
	 */
	verificationOldPhoneAsync:(data)=>requesthttp('POST', `${lk_buyer}/buyer/passport/member/verificationOldPhone`,data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	 * @param { }
	 * @description 修改新的手机号
	 */
	editPhoneAsync:(data)=>requesthttp('POST', `${lk_buyer}/buyer/passport/member/editPhone`,data, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
}

